<?php
class SlideShow
{
    private $images = array();
    private $navigation = '';
    private $currentImg = '';
    
    public function __construct()
    {
    }
    
    private function getArrayOfImages()
    {
        $query = DataBaseProvider::getAllImages();
        $this->images = DataBase::getInstance()->query( $query );
    }
    
    private function connectArrayBorder( $currentPos, $arrayLength )
    {
        if ( $currentPos == 0 )
        {
            return $this->images[$arrayLength-1]['file_name'];
        }
        else
        {
            return $this->images[0]['file_name'];
        }
    }
    
    private function findImage()
    {
        if ( empty( $this->navigation ) )
        {
            $this->navigation = 'next';
        }
        $arrayLength = count( $this->images );
        for ( $i = 0; $i < $arrayLength; ++$i )
        {
            if ( $this->images[$i]['file_name'] == $this->currentImg )
            {
                $imageName    = '';
                $isArrayLimit = ( ( $this->navigation == 'prev' ) && ( $i == 0 ) ) ||
                                ( ( $this->navigation == 'next' ) && ( ( $i + 1 ) == $arrayLength ) );
                if ( $isArrayLimit )
                {
                    $imageName = $this->connectArrayBorder( $i, $arrayLength );
                }
                elseif ( $this->navigation == 'next' )
                {
                    $imageName = $this->images[$i+1]['file_name'];
                }
                elseif ( $this->navigation == 'prev' )
                {
                    $imageName = $this->images[$i-1]['file_name'];
                }
                return $imageName;
            }
        }
    }

    public function getImageName( $currentImg, $navigation )
    {
        $this->currentImg = $currentImg;
        $this->navigation = strtolower( $navigation );
        $this->getArrayOfImages();
        if ( empty( $this->images ) )
        {
            return 'no images';
        }
        if ( empty( $this->currentImg ) )
        {
            return $this->images[0]['file_name'];
        }
        return $this->findImage();
    }
}
?>